using System.Linq;
using MallSystembackend.Api.Model;
using MallSystembackend.Api.ParamModel;
using MallSystembackend.Api.Repository;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace MallSystembackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class CommodityController : ControllerBase
    {
        public IRepository<Commodity> _commodityRepository;

        public IRepository<CommodityType> _commodityTypeRepository;
        public IRepository<Merchant> _merchantRepository;

        public readonly IConfiguration _configuration;

        public CommodityController(
            IRepository<Commodity> commodityRepository,
            IRepository<Merchant> merchantRepository,
            IRepository<CommodityType> commodityTypeRepository,
            IConfiguration configuration
        )
        {
             _commodityTypeRepository = commodityTypeRepository;
            _commodityRepository = commodityRepository;
            _configuration = configuration;
            _merchantRepository = merchantRepository;
        }

        //商品是否上架
        [HttpPut, Route("statu/{id}")]
        public ActionResult PutStatu(ParamStatus model, int id)
        {
            var commodity = _commodityRepository.GetById(id);
            if (commodity != null)
            {
                if (model.isActived == false)
                {
                    commodity.IsActived = true;
                    _commodityRepository.Update (commodity);
                    return Ok(new {
                        Code = 1000,
                        Data = commodity,
                        Msg = "修改成功"
                    });
                }
                else
                {
                    commodity.IsActived = false;
                    _commodityRepository.Update (commodity);
                    return Ok(new {
                        Code = 1000,
                        Data = commodity,
                        Msg = "修改成功"
                    });
                }
            }
            return Ok(new {
                Code = 1002,
                Data = "",
                Msg = "获取失败，请检查id是否正确"
            });
        }

        //添加商品
        [HttpPost]
        public ActionResult AddCommodity(ParamCommodity model)
        {
            var commodity =
                new Commodity {
                    CommodityName = model.CommodityName,
                    CommodityTypeId = model.CommodityTypeId,
                    MerchantId = model.MerchantId,
                    CommodityMoney = model.CommodityMoney
                };
            _commodityRepository.Insert (commodity);
            return Ok(new { Code = 1000, Data = commodity, Msg = "添加成功" });
        }

        //根据店主id获取他店铺的所有商品
        [HttpGet("GetCommodityById/{id}")]
        public ActionResult  GetCommodityByUserId(int id)
        {
            var merchants =
                _merchantRepository
                    .Table
                    .Where(x => x.UsersId == id && x.IsDeleted == false);

            var commoditys =
                _commodityRepository.Table.Where(x => x.IsDeleted == false);


            var commoditype = _commodityTypeRepository.Table.Where(x =>x.IsActived == true);

            var commodity =
                (
                from comd in commoditys
                join mer in merchants on comd.MerchantId equals mer.Id
                join type in commoditype on comd.CommodityTypeId equals type.Id
                select new{comd,type.TypeName,mer.MerchantName}
                ).ToList();

            

            return Ok(new {
                Code = 1000,
                Data =
                    commodity,
                Msg = "获取成功"
            });
        }

        //根据商品id查找
        [HttpGet("GetId/{id}")]
        public ActionResult GetCommodityId(int id)
        {
            var commoditys = _commodityRepository.GetById(id);
            return Ok(new { Code = 1000, Data = commoditys, Msg = "获得成功" });
        }

        //根据不同的商品分类获得不同的商品
        [HttpGet("GetType/{id}")]
        public ActionResult GetTypeId(int id)
        {
            var commoditys = _commodityRepository.Table;
            var Commodity = commoditys.Where(x => x.CommodityTypeId == id);
            return Ok(new { Code = 1000, Data = Commodity, Msg = "获得成功" });
        }

        //根据店铺id获取所有商品
        [HttpGet("id")]
        public ActionResult GetAllCommodtyById(int id)
        {
            
            var commiditys =
                _commodityRepository
                    .Table
                    .Where(x => x.MerchantId == id)
                    .ToList();
            return Ok(new { Code = 1000, Data = commiditys, Msg = "获得成功" });
        }

        //获得所有商品
        [HttpGet]
        public ActionResult GetCommodity()
        {
            var commoditys = _commodityRepository.Table;
            return Ok(new { Code = 1000, Data = commoditys, Msg = "获得成功" });
        }

        //更改商品信息
        [HttpPut("{id}")]
        public ActionResult UpdateCommodity(ParamCommodity model, int id)
        {
            var commodity = _commodityRepository.GetById(id);
            if (commodity != null)
            {
                commodity.CommodityName = model.CommodityName;
                commodity.CommodityTypeId = model.CommodityTypeId;
                commodity.MerchantId = model.MerchantId;
                commodity.CommodityMoney = model.CommodityMoney;
            }
            _commodityRepository.Update (commodity);
            return Ok(new { Code = 1000, Data = commodity, Msg = "修改成功" });
        }

        //删除商品
        [HttpDelete("{id}")]
        public ActionResult DeletedCommodity(int id)
        {
            var Commodity = _commodityRepository.GetById(id);
            if (Commodity != null)
            {
                Commodity.IsDeleted = true;
                _commodityRepository.Update (Commodity);
                return Ok(new {
                    Code = 1000,
                    Data = Commodity,
                    Msg = "删除成功"
                });
            }
            return Ok(new { Code = 1002, Data = Commodity, Msg = "删除失败" });
        }

        //根据当前店铺拥有商品名模糊查询
        [HttpGet("SelectCommodityByMerchantId/{id}")]
        public ActionResult GetCommodityName(ParamCommodity model, int id)
        {
            var merchants =
                _merchantRepository
                    .Table
                    .Where(x => x.UsersId == id && x.IsDeleted == false);

            var commoditys =
                _commodityRepository.Table.Where(x => x.IsDeleted == false);

            var commodity =
                (
                from comd in commoditys
                join mer in merchants on comd.MerchantId equals mer.Id
                select comd
                ).ToList();

            if (commodity != null)
            {
                if (model.MerchantId == 0)
                {
                    return Ok(new {
                        Code = 1000,
                        Data =
                            commodity
                                .Where(x =>
                                    x
                                        .CommodityName
                                        .Contains(model.CommodityName))
                                .Where(x => x.IsDeleted == false),
                        Msg = "获得成功"
                    });
                }else{
                    return Ok(new {
                        Code = 1000,
                        Data =
                            commodity
                                .Where(x =>
                                    x
                                        .CommodityName
                                        .Contains(model.CommodityName)
                                        && x.MerchantId == model.MerchantId
                                        )
                                .Where(x => x.IsDeleted == false),
                        Msg = "获得成功"
                    });
                }
            }
            else
            {
                return Ok(new { Code = 1000, Data = " ", Msg = "获得失败" });
            }
        }

        //根据商品名模糊查询
        [HttpGet("CommodityNames")]
        public ActionResult GetCommodityName(ParamCommodity model)
        {
            var commoditys = _commodityRepository.Table;
            var commodity =
                commoditys
                    .Where(x => x.CommodityName.Contains(model.CommodityName))
                    .Where(x => x.IsDeleted == false)
                    .ToList();

            if (commodity != null)
            {
                return Ok(new {
                    Code = 1000,
                    Data = commodity,
                    Msg = "获得成功"
                });
            }
            else
            {
                return Ok(new { Code = 1000, Data = " ", Msg = "获得失败" });
            }
        }
    }
}
